//
//  JSBoxViewCell.swift
//  fho
//
//  Created by 郭颢源 on 2021/3/25.
//

import UIKit

class JSBoxViewCell: JSBaseCollectionCell,
                     UICollectionViewDataSource,
                     UICollectionViewDelegateFlowLayout {
    let FXBoxItemCellID:String = "FXBoxItemCell"
    let kItemHeight:CGFloat = 40
    /// 元素的大小
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let item:JSBoxItem = self.model.items[indexPath.item]
        return CGSize(width: item.width, height: kItemHeight)
    }

    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.model.items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:JSBoxItemCell = collectionView.dequeueReusableCell(withReuseIdentifier: FXBoxItemCellID, for: indexPath) as! JSBoxItemCell
        cell.boxItem = self.model.items[indexPath.item]
        
        return cell
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubview(self.collectionView)

        collectionView.dataSource = self
        collectionView.delegate = self
        
        self.collectionView.register(JSBoxItemCell.self, forCellWithReuseIdentifier: FXBoxItemCellID)
        self.collectionView.isUserInteractionEnabled = !self.model.showInfo
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func initWith<T>(Model formModel: JSFormModel, VC ViewController: T) where T : BaseFormsController {
        super.initWith(Model: formModel, VC: ViewController)
        for item in self.model.items {
            if item.width == 0.0 {

                item.width = self.iconBtnImageWidth + widthWithFont(self.textFont, constrainedToHeight: kItemHeight, text: item.name) + 4//4 = space1
            }
        }
    }

    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
